package com.qianfeng.qlm.handforireader.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.lidroid.xutils.BitmapUtils;
import com.qianfeng.qlm.handforireader.R;
import com.qianfeng.qlm.handforireader.model.Category;
import com.qianfeng.qlm.handforireader.util.Constant;

import java.util.List;

/**
 * Created by jiangfeng on 2015/3/24.
 */
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> implements View.OnTouchListener {

    private static final String TAG = "CategoryAdapter";

    private Context context;
    private List<Category> data;
    private View.OnClickListener listener;
    private RequestQueue mQueue;

    public CategoryAdapter(Context context, List<Category> data, View.OnClickListener listener, RequestQueue mQueue) {
        this.context = context;
        this.data = data;
        this.listener = listener;
        this.mQueue = mQueue;
    }

    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_category, parent, false);
        view.setOnTouchListener(this);
        if (listener != null){
            view.setOnClickListener(listener);
        }
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Category category = data.get(position);
        holder.categoryName.setText(category.getComic_tag_name());

        String url = Constant.URL_SUFFIX + category.getComic_pic_url();

        BitmapUtils bitmapUtils = new BitmapUtils(context);
        bitmapUtils.configDefaultLoadingImage(R.mipmap.homebg);
        bitmapUtils.display(holder.categoryImage,url);

//        ImageRequest imageRequest = new ImageRequest(url,new Response.Listener<Bitmap>() {
//            @Override
//            public void onResponse(Bitmap response) {
//                holder.categoryImage.setImageBitmap(response);
//            }
//        },200,200, Bitmap.Config.ARGB_8888,new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//                Log.d(TAG,"下载图片出错");
//            }
//        });
//        mQueue.add(imageRequest);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        CardView cardView = (CardView) v;
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                cardView.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.card_elevation_pass));
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                cardView.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.card_elevation));
                break;
        }
        return listener == null;
    }


    public static class ViewHolder extends RecyclerView.ViewHolder{

        private ImageView categoryImage;
        private TextView categoryName;

        public ViewHolder(View itemView) {
            super(itemView);
            categoryImage = (ImageView) itemView.findViewById(R.id.category_image);
            categoryName = (TextView) itemView.findViewById(R.id.category_name);
        }
    }

}
